//
//  WXLiveFinishView.m
//  66TV
//
//  Created by 石锋 on 16/8/5.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "WTLiveFinishView.h"
#import "SDImageCache.h"
#import "WTCategories.h"


@interface WTLiveFinishView ()

@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *attendanceLab;

@property (weak, nonatomic) IBOutlet UIButton *attentionAnchorBt;
@property (weak, nonatomic) IBOutlet UIButton *goSquareBt;
@property (weak, nonatomic) IBOutlet UILabel *tuiJianLb;
@property (weak, nonatomic) IBOutlet UILabel *overLive;


@end

@implementation WTLiveFinishView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 37.5;
    self.goSquareBt.layer.cornerRadius = 5;
    self.goSquareBt.layer.masksToBounds = YES;
    
    self.attentionAnchorBt.layer.cornerRadius = 5;
    self.attentionAnchorBt.layer.masksToBounds = YES;
    self.goSquareBt.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
    
    self.backgroundImageView.userInteractionEnabled = YES;
    
}

- (IBAction)attentionAnchorBtDidPress:(id)sender {
    if(self.finishedViewBlock){
        self.finishedViewBlock(0);
    }
}

- (IBAction)goSquareBtDidPress:(id)sender {
    if(self.finishedViewBlock){
        self.finishedViewBlock(1);
    }
}

- (void)update:(NSString *)avatar name:(NSString *)name{
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage wt_imageWithName:@"wt_avatar"]];
    
    self.nameLab.text = name;
    
    self.backgroundImageView.image = [[UIImage imageNamed:@"wt_live_background_image"] blurredImageWithRadius:50 iterations:2 tintColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];
    
    UIImage* backImage = nil;
    if(avatar){
        backImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:avatar];
        if(!backImage){
            
            __weak __typeof(self)weakSelf = self;
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:avatar] options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                
                if (image) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf animate:image];
                    });
                }
            }];
        }else {
            [self animate:backImage];
        }
    }else {
        [self animate:[UIImage imageNamed:@"wt_live_background_image"]];
    }
    
}

- (void)animate:(UIImage*)image{
    self.backgroundImageView.image = [image blurredImageWithRadius:50 iterations:2 tintColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.25f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.backgroundImageView.layer addAnimation:transition forKey:nil];
}

- (void)update:(NSString*)roomID{
    
//    [self makeRequestForType:ServiceType_RoomInfo params:@{@"room_id":@([roomID longLongValue]),@"session_token":WTConfig.session_token} method:@"POST" userInfo:nil];
}

- (void)setIs_focoused:(BOOL)is_focoused{
    
    _is_focoused = is_focoused;
    if(is_focoused){
        [self.attentionAnchorBt setTitle:@"已关注" forState:UIControlStateNormal];
    }else {
        [self.attentionAnchorBt setTitle:@"关注主播  ，不迷路！" forState:UIControlStateNormal];
    }
}

@end
